﻿using System;
using System.Threading.Tasks;
using Volo.Abp;
using Volo.Abp.Domain.Repositories;
using Xms.Abp.Domain;

namespace IdentityManagement
{
    public class IdentityClaimTypeManager : XmsDomainService
    {
        protected IIdentityClaimTypeRepository IdentityClaimTypeRepository => LazyInject<IIdentityClaimTypeRepository>();

        public virtual async Task<IdentityClaimType> CreateAsync(IdentityClaimType claimType)
        {
            if (await IdentityClaimTypeRepository.AnyAsync(claimType.Name))
            {
                throw new AbpException($"Name Exist: {claimType.Name}");
            }

            return await IdentityClaimTypeRepository.InsertAsync(claimType);
        }

        public virtual async Task<IdentityClaimType> UpdateAsync(IdentityClaimType claimType)
        {
            if (await IdentityClaimTypeRepository.AnyAsync(r => r.Name == claimType.Name && r.Id == claimType.Id))
            {
                throw new AbpException($"Name Exist: {claimType.Name}");
            }

            if (claimType.IsStatic)
            {
                throw new AbpException($"Can not update a static ClaimType.");
            }


            return await IdentityClaimTypeRepository.UpdateAsync(claimType);
        }
    }
}
